/*
 * @(#) LtcmExample.java  1.0  2004-4-6
 *
 * Copyright 2003 by LongTop Corporation.
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of
 * LongTop Corporation ("Confidential Information").  You
 * shall not disclose such Confidential Information and shall use
 * it only in accordance with the terms of the license agreement
 * you entered into with LongTop.
 */

package com.cnc.bas.dao;

import java.util.*;

import org.hibernate.EntityMode;
import org.hibernate.HibernateException;
import org.hibernate.criterion.Example;
import org.hibernate.engine.SessionFactoryImplementor;
import org.hibernate.metadata.ClassMetadata;
import org.hibernate.type.AbstractComponentType;
import org.hibernate.type.Type;
import org.hibernate.util.StringHelper;

/**
 * @author Du bf
 * @version 1.0  2004-4-6
 */
public class LtcmExample extends Example {
    public LtcmExample(Object o, Example.PropertySelector propertySelector) {
        super(o, propertySelector);
        this.ltcmEntity = o;
        //this.selector = propertySelector;
    }

    private Object ltcmEntity;
    /**
     * A strategy for choosing property values for inclusion in the query
     * criteria
     */
//    private static final PropertySelector NOT_EMPTY = new NotEmptyPropertySelector();

    /**
     * Create a new instance, which includes all non-null properties
     * by default
     *
     * @param entity
     * @return a new instance of <tt>Example</tt>
     */
//    public static Example create(Object entity) {
//        if (entity == null) throw new NullPointerException("null example");
//
//        return new LtcmExample(entity, NOT_EMPTY);
//    }
//
//    private final Set excludedProperties = new HashSet();
//    private PropertySelector selector;
//
//    public Example excludeProperty(String name) {
//        excludedProperties.add(name);
//        return this;
//    }
//
//    private boolean isPropertyIncluded(Object value, String name, Type type) {
//        return !excludedProperties.contains(name) &&
//                !type.isAssociationType() &&
//                selector.include(value, name, type);
//    }
//
//    public String toSqlString(SessionFactoryImplementor sessionFactory,
//                              Class persistentClass,
//                              String alias,
//                              Map aliasClasses)
//            throws HibernateException {
//
//        StringBuffer buf = new StringBuffer().append("(");
//        ClassMetadata meta = sessionFactory.getClassMetadata(persistentClass);
//        String[] propertyNames1 = meta.getPropertyNames();
//        Type[] propertyTypes1 = meta.getPropertyTypes();
//        Object[] propertyValues1 = meta.getPropertyValues(ltcmEntity, EntityMode.POJO);
//
//        String idname = meta.getIdentifierPropertyName();
//        Object idValue = meta.getIdentifier(ltcmEntity, EntityMode.POJO);
//        Type idType = meta.getIdentifierType();
//
//        String[] propertyNames = this.appendStringArray(propertyNames1, idname);
//        Type[] propertyTypes = this.appendTypeArray(propertyTypes1, idType);
//        Object[] propertyValues = this.appendObjectArray(propertyValues1, idValue);
//        for (int i = 0; i < propertyNames.length; i++) {
//            Object propertyValue = propertyValues[i];
//            String propertyName = propertyNames[i];
//
//            boolean isPropertyIncluded = i != meta.getVersionProperty() &&
//                    isPropertyIncluded(propertyValue, propertyName, propertyTypes[i]);
//            if (isPropertyIncluded) {
//                if (propertyTypes[i].isComponentType()) {
//                    appendComponentCondition(propertyName,
//                            propertyValue,
//                            (AbstractComponentType) propertyTypes[i],
//                            persistentClass,
//                            alias,
//                            aliasClasses,
//                            sessionFactory,
//                            buf);
//                } else {
//                    appendPropertyCondition(propertyName,
//                            propertyValue,
//                            persistentClass,
//                            alias,
//                            aliasClasses,
//                            sessionFactory,
//                            buf);
//                }
//            }
//        }
//        if (buf.length() == 1) buf.append("1=1"); //yuck!
//        return buf.append(")").toString();
//    }
//
//    private static final Object[] TYPED_VALUES_LTCM = new TypedValue[0];
//
//    public TypedValue[] getTypedValues(SessionFactoryImplementor sessionFactory,
//                                       Class persistentClass,
//                                       Map aliasClasses)
//            throws HibernateException {
//
//        ClassMetadata meta = sessionFactory.getClassMetadata(persistentClass);
//        String[] propertyNames1 = meta.getPropertyNames();
//        Type[] propertyTypes1 = meta.getPropertyTypes();
//        Object[] propertyValues1 = meta.getPropertyValues(ltcmEntity);
//
//        String idname = meta.getIdentifierPropertyName();
//        Object idValue = meta.getIdentifier(ltcmEntity);
//        Type idType = meta.getIdentifierType();
//
//        String[] propertyNames = this.appendStringArray(propertyNames1, idname);
//        Type[] propertyTypes = this.appendTypeArray(propertyTypes1, idType);
//        Object[] propertyValues = this.appendObjectArray(propertyValues1, idValue);
//        List list = new ArrayList();
//        for (int i = 0; i < propertyNames.length; i++) {
//            Object value = propertyValues[i];
//            Type type = propertyTypes[i];
//            String name = propertyNames[i];
//
//            boolean isPropertyIncluded = i != meta.getVersionProperty() &&
//                    isPropertyIncluded(value, name, type);
//
//            if (isPropertyIncluded) {
//                if (propertyTypes[i].isComponentType()) {
//                    addComponentTypedValues(name, value, (AbstractComponentType) type, list);
//                } else {
//                    addPropertyTypedValue(value, type, list);
//                }
//            }
//        }
//        return (TypedValue[]) list.toArray(TYPED_VALUES_LTCM);
//    }
//
//    private String[] appendStringArray(String[] str, String astr) {
//        int len = str.length;
//        String[] result = new String[len + 1];
//        for (int i = 0; i < len; i++) {
//            result[i] = str[i];
//        }
//        result[len] = astr;
//        return result;
//    }
//
//    private Type[] appendTypeArray(Type[] str, Type astr) {
//        int len = str.length;
//        Type[] result = new Type[len + 1];
//        for (int i = 0; i < len; i++) {
//            result[i] = str[i];
//        }
//        result[len] = astr;
//        return result;
//    }
//
//    private Object[] appendObjectArray(Object[] str, Object astr) {
//        int len = str.length;
//        Object[] result = new Object[len + 1];
//        for (int i = 0; i < len; i++) {
//            result[i] = str[i];
//        }
//        result[len] = astr;
//        return result;
//    }
//
//    static final class NotEmptyPropertySelector implements PropertySelector {
//        public boolean include(Object object, String propertyName, Type type) {
//            return (object != null) && (object.toString().length() > 0);
//        }
//    }
}
